package cn.iocoder.yudao.module.agent.controller.admin.brokeragerecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agent.controller.admin.brokeragerecord.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokeragerecord.AgentBrokerageRecordDO;
import cn.iocoder.yudao.module.agent.convert.brokeragerecord.AgentBrokerageRecordConvert;
import cn.iocoder.yudao.module.agent.service.brokeragerecord.AgentBrokerageRecordService;

@Tag(name = "管理后台 - 代理人佣金记录")
@RestController
@RequestMapping("/agent/brokerage-record")
@Validated
public class AgentBrokerageRecordController {

    @Resource
    private AgentBrokerageRecordService brokerageRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建代理人佣金记录")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:create')")
    public CommonResult<Long> createBrokerageRecord(@Valid @RequestBody AgentBrokerageRecordCreateReqVO createReqVO) {
        return success(brokerageRecordService.createBrokerageRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理人佣金记录")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:update')")
    public CommonResult<Boolean> updateBrokerageRecord(@Valid @RequestBody AgentBrokerageRecordUpdateReqVO updateReqVO) {
        brokerageRecordService.updateBrokerageRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理人佣金记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:delete')")
    public CommonResult<Boolean> deleteBrokerageRecord(@RequestParam("id") Long id) {
        brokerageRecordService.deleteBrokerageRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理人佣金记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:query')")
    public CommonResult<AgentBrokerageRecordRespVO> getBrokerageRecord(@RequestParam("id") Long id) {
        AgentBrokerageRecordDO brokerageRecord = brokerageRecordService.getBrokerageRecord(id);
        return success(AgentBrokerageRecordConvert.INSTANCE.convert(brokerageRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得代理人佣金记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:query')")
    public CommonResult<List<AgentBrokerageRecordRespVO>> getBrokerageRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<AgentBrokerageRecordDO> list = brokerageRecordService.getBrokerageRecordList(ids);
        return success(AgentBrokerageRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理人佣金记录分页")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:query')")
    public CommonResult<PageResult<AgentBrokerageRecordRespVO>> getBrokerageRecordPage(@Valid AgentBrokerageRecordPageReqVO pageVO) {
        PageResult<AgentBrokerageRecordDO> pageResult = brokerageRecordService.getBrokerageRecordPage(pageVO);
        return success(AgentBrokerageRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理人佣金记录 Excel")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-record:export')")
    @OperateLog(type = EXPORT)
    public void exportBrokerageRecordExcel(@Valid AgentBrokerageRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AgentBrokerageRecordDO> list = brokerageRecordService.getBrokerageRecordList(exportReqVO);
        // 导出 Excel
        List<AgentBrokerageRecordExcelVO> datas = AgentBrokerageRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "代理人佣金记录.xls", "数据", AgentBrokerageRecordExcelVO.class, datas);
    }

}
